﻿using UnityEngine;
using FairyGUI;

public class FCard : GButton
{
    GLoader _loader;
    GLoader _backLoader;
    GLoader _elementLoader;
    Card _cardData;
    GLabel _discriptLabel;
    GLabel _needLabel;
    GLabel _nameLabel;

    public override void ConstructFromXML(FairyGUI.Utils.XML cxml)
    {
        base.ConstructFromXML(cxml);

        _loader = ((GComponent)this.GetChild("card")).GetChild("imgCom").asCom.GetChild("img").asLoader;
        _backLoader = ((GComponent)this.GetChild("card")).GetChild("cardBackground").asLoader;
        _elementLoader = this.GetChild("elementIcon").asLoader;
        _discriptLabel = this.GetChild("discriptLabel").asLabel;
        _needLabel = this.GetChild("needLabel").asLabel;
        _nameLabel = this.GetChild("nameLabel").asLabel;
    }

    public void InitData(Card cardData,bool isShowClickAni = true)
    {
        if (!isShowClickAni) _loader.parent.touchable = false;
        else _loader.parent.touchable = true;
        _cardData = cardData;
        Fresh();
    }

    public void Fresh(int level = 0)
    {
        if (_cardData == null) return;
        _loader.url = _cardData.img;
        //设置背景图片
        string backUrl = "Cardface_";
        switch(_cardData.type){
            case GMConfig.CardType.Common:
                backUrl += "Common";
                break;
            case GMConfig.CardType.Keep:
                backUrl += "Keep";
                break;
            case GMConfig.CardType.Final:
                backUrl += "Final";
                break;
            case GMConfig.CardType.Special:
                backUrl += "Special";
                break;
        }
        _backLoader.url = UIPackage.GetItemURL("CommonPackage",backUrl);
        //设置元素图片
        string elementkUrl = "";
        switch(_cardData.elementType){
            case GMConfig.ElementType.IceElement:
                elementkUrl += "Ice";
                break;
            case GMConfig.ElementType.FireElement:
                elementkUrl += "Fire";
                break;
            case GMConfig.ElementType.ToxicElement:
                elementkUrl += "Toxic";
                break;
        }
        _elementLoader.url = UIPackage.GetItemURL("CommonPackage",elementkUrl);
        _nameLabel.text = _cardData.GetCardName();
        //默认展示1级
        var discript = _cardData.GetPropertyDiscript(level);
        if (_discriptLabel.text != discript) _discriptLabel.text = discript;
        var needDiscript = _cardData.GetNeedDiscript(level);
        if (_needLabel.text != needDiscript) _needLabel.text = needDiscript;
    }

    public void SetImg(string url)
    {
        _loader.url = url;
    }

    public string GetCardImg()
    {
        return _loader.url;
    }
}

